class MeasureAreMa {
	constructor(finshCallback) {
		viewer.scene.globe.depthTestAgainstTerrain = true;
		finshCallback = (res) => {
			return res.toFixed(2);
		};
		this.ignoreroot = viewer.graphicLayer.getIgnoreRoot();
		this.finshCallback = finshCallback;
		this.group = new GV.GraphicGroup();
		viewer.graphicLayer.add(this.group);
		this._selectPoints = [];
		this._selectPointGraphic = [];
		viewer.screenSpaceEventHandler.setInputAction((movement) => {
			const pickObj = viewer.graphicLayer.pickByCoordinate2(movement.position.x, movement.position.y);
			// console.log(pickObj)
			if (this.closeLabel && pickObj[0] === this.closeLabel) {
				this.group.remove(this.closeLabel);
				this.closeLabel = undefined;
				if (this._labelItem) {
					this.ignoreroot.remove(this._labelItem);
					this._labelItem = undefined;
				}
			}
		}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
	}
	// 淹没范围点设置
	selectPoint() {
		this.close();
		this._selectPoints = [];
		this._selectPointGraphic = [];
		if (this._selectPoints.length === 0) {
			const polygon = [];
			const viewer = window.viewer;
			let handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
			handler.setInputAction(event => {
				// 屏幕坐标转世界坐标——关键点
				let ray = viewer.camera.getPickRay(event.position);
				let cartesian = viewer.scene.globe.pick(ray, viewer.scene);
				// 将笛卡尔坐标转换为地理坐标
				let cartographic = Cesium.Cartographic.fromCartesian(cartesian);
				// 将弧度转为度的十进制度表示
				let lon = Cesium.Math.toDegrees(cartographic.longitude);
				let lat = Cesium.Math.toDegrees(cartographic.latitude);
				// 获取海拔高度
				let alt = viewer.scene.globe.getHeight(cartographic);
				let height2 = cartographic.height;
				const point = new GV.GeoPoint(lon, lat, alt);
				// console.log(point)
				polygon.push(point);
				this._selectPoints.push(point);
				this._selectPointGraphic.push(
					viewer.graphicLayer.add(
						new GV.PointGraphic({
							position: point,
							size: 0.2, // 大小,
							color: '#ffff00'
						})
					)
				);
				if (!this._polygon) {
					this._polygon = window.viewer.graphicLayer.add(
						new GV.PolygonGraphic({
							positions: this._selectPoints, // 设置坐标
							color: 'rgba(0,153,255,0.6)', // 填充色
							clampToGround: this._clampToGround
						})
					);
				} else {
					this._polygon.positions = this._selectPoints;
				}
				if (this._selectPoints.length < 2) { return }
				if (!this.labelGraphicOpt) {
					this.labelGraphicOpt = new GV.LabelGraphicOpt();
					this.labelGraphicOpt.position = new GV.GeoPoint(0, 0, 0);
					this.labelGraphicOpt.horizontalOrigin = 'right';
					this.labelGraphicOpt.verticalOrigin = 'bottom';
					// this.labelGraphicOpt.style = style
					this.labelGraphic = new GV.LabelGraphic(this.labelGraphicOpt);
					this._labelItem = this.ignoreroot.add(this.labelGraphic);
				}
				this._calcDistance(this._selectPoints, this.finshCallback);
				this._labelItem.position = this._selectPoints[this._selectPoints.length - 1];
				this._labelItem.text = this._area;
			}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
			handler.setInputAction(() => {
				if (polygon.length > 3) {
					// _selectPoints = graphicLayer.getItemsInPolygon(polygon)
					// 结束点选
					if (!handler.isDestroyed()) {
						handler.destroy();
						handler = null;
					}
				} else {
					console.log('多边形数量点不够');
				}
				this._selectPointGraphic.forEach(item => {
					window.viewer.graphicLayer.remove(item);
				});
				this._selectPointGraphic = [];
				this.closeLabel = new GV.BillboardGraphic({
					type: 'billboard',
					position: this._labelItem.position,
					image: 'images/baseMapImg/关闭.png',
					width: 20,
					height: 20
				});
				this.group.add(this.closeLabel);
			}, Cesium.ScreenSpaceEventType.RIGHT_DOWN);
			handler.setInputAction(function () {
				handler.destroy();
			}, Cesium.ScreenSpaceEventType.RIGHT_UP);
		}
	}
	// 清除所有的淹没范围点
	clearGraphic() {
		this._selectPointGraphic.forEach(item => {
			window.viewer.graphicLayer.remove(item);
		});
		window.viewer.graphicLayer.remove(this._polygon);
		this._selectPointGraphic = [];
		this._selectPoints = [];
		this._polygon = undefined;
		if (this.closeLabel) {
			this.group.remove(this.closeLabel);
			this.closeLabel = undefined;
		}
	}
	// 设置淹没基准点
	selectBsaePoint() {
		const ignoreroot = window.viewer.graphicLayer.getIgnoreRoot();
		if (!this._handler) {
			this._handler = new Cesium.ScreenSpaceEventHandler(window.viewer.scene.canvas);
		}
		this._handler.setInputAction(event => {
			const position = event.position;
			// 根据屏幕坐标获取坐标位置
			const point = GV.GeoPoint.fromScreen(position.x, position.y, window.viewer);
			if (!point) return;
			// if (this._point1) return
			const point1 = new GV.PointGraphic({
				position: point,
				color: '#f66',
				pixelSize: 10
			});
			this._point1 = this.group.add(point1);
			const labelGraphicOpt = new GV.LabelGraphicOpt();
			labelGraphicOpt.position = point1.position;
			labelGraphicOpt.text = '淹没基准点';
			labelGraphicOpt.font = '20px Helvetica';
			labelGraphicOpt.fillColor = '#fff';
			labelGraphicOpt.horizontalOrigin = 'left';
			labelGraphicOpt.verticalOrigin = 'bottom';
			const labelGraphic = new GV.LabelGraphic(labelGraphicOpt);
			this._label1 = ignoreroot.add(labelGraphic);
			console.log(this._label1);
		}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
	}

	// 淹没分析
	doAnalysis() {
		if (!this._point1) return;
		const polygon = new GV.PolygonGraphic({
			positions: this._polygon.positions, // 设置坐标
			clampToGround: true,
			extrudedHeight: 0,
			color: 'rgba(0,153,255,0.0)' // 填充色
		});
		const ignoreroot = window.viewer.graphicLayer.getIgnoreRoot();
		this._polygonFlood = this.group.add(polygon); // 添加到场景
		let num = this._point1.position.alt / 8;
		const interval = setInterval(() => {
			polygon.color = 'rgba(0,153,255,0.6)'; // 填充色
			if (num < this._point1.position.alt) {
				num += this._point1.position.alt / 8;
				polygon.extrudedHeight = num;
			} else {
				clearInterval(interval);
			}
		}, 100);
	}
	// 测面的方法
	_calcDistance(positions, callbackFixed) {
		const len = positions.length;
		if (len < 2) return;
		let area = 0;
		if (this._clampToGround) {
			area = GV.Calculate.groundArea(positions);
		} else {
			area = GV.Calculate.spaceArea(positions);
		}
		let unit = 'm2';
		if ((String(area)).split('.')[0].length > 8) {
			unit = 'kilometer';
		}
		area = Math.abs(area);
		switch (unit) {
			case 'kilometer':
				this._area = area / 1000000;
				this._area = callbackFixed(this._area) + 'km2';
				break;
			default:
				this._area = area;
				this._area = callbackFixed(this._area) + 'm2';
				break;
		}
	}
	// 关闭分析
	close() {
		this.clearGraphic();
		if (this._handler && !this._handler.isDestroyed()) {
			this._handler.destroy();
			this._handler = null;
		}
		const ignoreroot = viewer.graphicLayer.getIgnoreRoot();
		if (this._polygon) {
			ignoreroot.remove(this._ploygon);
			this._polygon = undefined;
		}
		if (this._labelItem) {
			ignoreroot.remove(this._labelItem);
			this._labelItem = undefined;
		}
		if (this._label1) {
			ignoreroot.remove(this._label1);
			this._label1 = undefined;
		}
		this.labelGraphicOpt = undefined;
		this.group.clear();
	}
}
export {
	MeasureAreMa
};
